//
//  BaseNavigationBar.m
//  SleepApp
//
//  Created by chuangqi on 2021/4/6.
//  Copyright © 2021 RiceWang. All rights reserved.
//

#import "BaseNavigationBar.h"


@interface BaseNavigationBar ()
/** 中间内容 */
@property (nonatomic,strong) UIView *titleView;
/** 左边按钮 */
@property (nonatomic,strong) UIView *leftView;
/** 右边按钮 */
@property (nonatomic,strong) UIView *rightView;
/** 导航栏下的横线 */
@property (nonatomic,strong) UIView *navigationLineView;
@end
@implementation BaseNavigationBar

- (instancetype)init{
    self = [super init];
    if (self) {
        self.backgroundColor = UIColor.whiteColor;
        [self addSubview:self.navigationLineView];
        [self.navigationLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.mas_equalTo(self);
            make.height.mas_offset(.5f);
        }];
        
        [self addSubview:self.leftView];
        [self addSubview:self.rightView];
        [self addSubview:self.titleView];
        [self.leftView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self).mas_offset(kStatusBarHeight);
            make.left.mas_equalTo(self).mas_offset(8);
            make.bottom.mas_equalTo(self);
        }];
        [self.rightView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self).mas_offset(kStatusBarHeight);
            make.right.mas_equalTo(self).mas_offset(-8);
            make.bottom.mas_equalTo(self);
        }];
        [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self).mas_offset(kStatusBarHeight);
            make.bottom.mas_equalTo(self);
            make.left.mas_equalTo(self.leftView.mas_right);
            make.right.mas_equalTo(self.rightView.mas_left);
        }];
    }
    return self;
}
-(void)setNavigationColor:(UIColor *)color navigationLienColor:(UIColor *)lienColor{
    self.backgroundColor = color;
    self.navigationLineView.backgroundColor = lienColor;
}
#pragma mark -- 组建插入
/** 设置标题视图 */
-(void)titleView:(UIView *)view{
    for (UIView *k in self.titleView.subviews) {
        [k removeFromSuperview];
    }
    [self.titleView addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
    [self layoutIfNeeded];
}
/** 设置标题内容 */
-(void)titileLabel:(UILabel *)label{
    for (UIView *k in self.titleView.subviews) {
        [k removeFromSuperview];
    }
    [self.titleView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.titleView);
        make.centerX.mas_equalTo(self);
    }];
    [self layoutIfNeeded];
}
/** 设置左侧按钮 */
-(void)leftBarButtonItems:(NSArray<UIButton *> *)items{
    UIButton *topButton = nil;
    for (UIButton *button in items) {
//        button.backgroundColor = UIColor.randomColor;
        [self.leftView addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.leftView).mas_offset(2);
            make.bottom.mas_equalTo(self.leftView).mas_offset(-2);
            make.width.mas_equalTo(button.mas_height);
            if (!topButton){
                make.left.mas_equalTo(self.leftView).mas_offset(2);
            } else {
                make.left.mas_equalTo(topButton.mas_right).mas_offset(2);
            }
            if (self.leftView.subviews.count == items.count) {
                make.right.mas_equalTo(self.leftView).mas_offset(-2);
            }
        }];
        topButton = button;
    }
    [self layoutIfNeeded];
}
/** 设置右侧按钮 */
-(void)rightBarButtonItems:(NSArray<UIButton *> *)items{
    UIButton *topButton = nil;
    for (UIButton *button in items) {
//        button.backgroundColor = UIColor.randomColor;
        [self.rightView addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.leftView).mas_offset(2);
            make.bottom.mas_equalTo(self.leftView).mas_offset(-2);
            make.width.mas_equalTo(button.mas_height);
            if (!topButton){
                make.right.mas_equalTo(self.rightView).mas_offset(2);
            } else {
                make.right.mas_equalTo(topButton.mas_left).mas_offset(-2);
            }
            if (self.rightView.subviews.count == items.count) {
                make.left.mas_equalTo(self.rightView).mas_offset(2);
            }
        }];
        topButton = button;
    }
    [self layoutIfNeeded];
}



#pragma mark --- 延迟加载
-(UIView *)titleView{
    if (!_titleView){
        _titleView = [[UIView alloc]init];
        _titleView.userInteractionEnabled = YES;
//        _titleView.backgroundColor = UIColor.whiteColor;
    }
    return _titleView;
}
-(UIView *)leftView{
    if (!_leftView){
        _leftView = [[UIView alloc]init];
        _leftView.userInteractionEnabled = YES;
    }
    return _leftView;
}
-(UIView *)rightView{
    if (!_rightView){
        _rightView = [[UIView alloc]init];
        _rightView.userInteractionEnabled = YES;
    }
    return _rightView;
}
-(UIView *)navigationLineView{
    if (!_navigationLineView) {
        _navigationLineView = [[UIView alloc]init];
        _navigationLineView.backgroundColor = [UIColor colorWithWhite:0 alpha:.1f];
    }
    return _navigationLineView;
}
@end
